package com.yunxi.service.localService.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.sys.AreaMapper;
import com.yunxi.dealer.pojo.AreaManagerVO;
import com.yunxi.dealer.service.AreaManagerDubboService;
import com.yunxi.model.sys.Area;
import com.yunxi.service.localService.sys.AreaService;
import com.yunxi.sys.pojo.AreaVO;
import com.yunxi.util.RedisCacheService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地域信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-07
 */
@Service
public class AreaServiceImpl extends ServiceImplPlus<AreaMapper, Area> implements AreaService {
   @Autowired
   AreaMapper areaMapper;

   @Autowired
   RedisCacheService redisCacheService;

   @Autowired
   AreaManagerDubboService areaManagerDubboServiceImpl;

   @Override
   public List<AreaVO> listByPid(String pid) {
      return areaMapper.listByPid(pid);
   }

   @Override
   public Object treeByPid() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      List<AreaVO> list = areaMapper.findAll(null);
      TreeConfig config = new TreeConfig("0", "行政区划", true, "code", "name", "pcode", "hasChild");
      Object o  = config.toTreeData(list,false);
      return o;
   }

   @Override
   public List<AreaVO> listProvince() {
      return areaMapper.listProvince();
   }

   @Override
   @Cacheable(value="area",key = "'area_type'+#type")
   public Object treeByType(Integer type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      TreeConfig config = new TreeConfig("0", "行政区划", true, "code", "name", "pcode", "hasChild");
      List<AreaVO> list = areaMapper.findAllByType(type);
      Object o  = config.toTreeData(list,false);
      return o;
   }

   @Override
   @Transactional
   public void saveBatchCas(AreaVO areaVO) throws Exception {
      //rcs.delete("dict:list_"+d.getDictId());
      //rcs.delete("dict:list_"+d.getPid());
      if (areaVO.getAreaVOList() != null && areaVO.getAreaVOList().size() > 0)
      {
         for (int i = 0; i < areaVO.getAreaVOList().size(); i++) {
            AreaVO m = areaVO.getAreaVOList().get(i);
            m.setType(1);
            if (StringUtils.isEmpty(areaVO.getAreaId())){
               m.setAreaId(null);
            }
            Area d2 = new Area();
            BeanUtils.copyProperties(m,d2);
            d2.setPcode(areaVO.getCode());
            LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Area::getCode,m.getCode());
            wrapper.eq(Area::getPcode,areaVO.getPcode());
            Integer count = areaMapper.selectCount(wrapper);
            if (count>0) throw new Exception(areaVO.getName()+"已存在该省份:"+m.getName());
            saveOrUpdate(d2);
         }
      }
      Area d = new Area();
      areaVO.setAreaId(areaVO.getCode());
      areaVO.setType(1);
      BeanUtils.copyProperties(areaVO,d);
      this.saveOrUpdate(d);
      redisCacheService.delete("area::area_type"+areaVO.getType());
   }

   @Override
   public Object loadCas(AreaVO areaVO) {
      return areaMapper.loadCas(areaVO);
   }

   @Override
   public void delCas(List<AreaVO> areaVOList) {
      if (areaVOList != null && areaVOList.size() > 0) {
         for (int i = 0; i < areaVOList.size(); i++) {
            AreaVO m = areaVOList.get(i);
            baseMapper.deleteById(m.getAreaId());
            baseMapper.deleteSub(m.getAreaId());
         }
      }
      redisCacheService.delete("area::area_type" + "1");
   }

   @Override
   public ApiResult listAllProvince(AreaVO a,LoginInfo loginInfo) {
      String userCode = null;
      if (a!=null && StringUtils.isNotEmpty(a.getCode())){
         userCode=a.getCode();
      }else {
         userCode = loginInfo.getUserCode();
      }
      AreaManagerVO vo = new AreaManagerVO();
      vo.setUserCode(userCode);
      vo.setPCode("0");
      List<AreaManagerVO> list = areaManagerDubboServiceImpl.listByVo(vo);
      if (list!=null && list.size()>0){
         AreaManagerVO areaManagerVO = list.get(0);
         AreaVO areaVO = new AreaVO();
         areaVO.setPcode(areaManagerVO.getAreaCode());
         areaVO.setType(1);
         List<AreaVO> areaVOList =  baseMapper.listAllProvince(areaVO);
         return ResponseUtil.ok(areaVOList);
      }else {
         return ResponseUtil.fail(-1,"请配置该大区经理管辖区域");
      }
   }

   @Override
   public PageInfo<AreaVO> findPageNotExit(AreaVO areaVo, Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<AreaVO> list =  baseMapper.findPageNotExit(areaVo.getPcode());
      PageInfo<AreaVO> pageInfo = new PageInfo<>(list);
      return pageInfo;
   }

   @Override
   public String getBigRegionByCityCode(String cityCode) {
      return baseMapper.getBigRegionByCityCode(cityCode);
   }

   @Override
   public AreaVO queryAreaByPartnerCode(String partnerCode) {
      return baseMapper.queryAreaByPartnerCode(partnerCode);
   }
}
